package com.eric.text;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.jsoup.nodes.Document;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.dic.Dictionary;

public class IKSegWorker extends AbstractTextWorker {

	Dictionary dictionary;
	Configuration conf;

	public IKSegWorker(Object input) {
		super(input);
	}

	protected void initThis() {

		conf = DefaultConfig.getInstance();
		conf.setUseSmart(true);// 智能模式

		if (customWords.size() > 0) {
			Dictionary.initial(conf);
			dictionary = Dictionary.getSingleton();
			dictionary.addWords(customWords);
		}
	}

	@Override
	public void produce() {
		init();
		initThis();
		seg();
	}

	protected IKSegWorker seg() {
		StringReader r = new StringReader(text);
		IKSegmenter iks = new IKSegmenter(r, conf);
		Lexeme lexeme;

		try {
			while ((lexeme = iks.next()) != null) {
				String s = lexeme.getLexemeText();
				if (acceptedText(s))
					result.add(s);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (removeStopWords)
			removeStopWords(result, stopWords);

		return this;
	}

	@Override
	public List<String> getContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> produce(String text, Document doc) {
		// TODO Auto-generated method stub
		return null;
	}
}
